/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.standard;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.just.dusin.ooii.component.Source;
import org.dusin.webcoal.exception.OOIIException;

/**
 * 每隔一定时间自动发送消息
 * @author Administrator
 */
public class AutoMessageSource extends Source {
    
    private static Logger logger = Logger.getLogger(AutoMessageSource.class.getName());
    /**
     * 重复运行次数
     */
    private int repeat;
    public int getRepeat() {
        return repeat;
    }
    public void setRepeat(int repeat) {
        this.repeat = repeat;
    }
   
    
    

   
    /**
     * 消息时间间隔，单位秒
     */
    private int interval = 10;

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }
   

    @Override
    public void initComponent(Properties props) {
    }

    @Override
    public void destroyComponent() throws Exception {
    }

    @Override
    public void run() {
        int i = 0;
        Date now = null;
        while (i<repeat) {
            i++;
            now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String timestamp = sdf.format(now);
            String msg = "" + i + " 你好，abc,abcd中文可abc以吗 " + timestamp;
            try {
                sink.process(msg);
                Thread.sleep(interval*1000);
            } catch (OOIIException ex) {
                logger.error(ex.getMessage());
            }catch(InterruptedException ie){
                
            }
        }
    }
}
